package com.lmabbe.core.service.impls;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmabbe.core.common.entitys.SysResource;
import com.lmabbe.core.common.entitys.SysRoleResource;
import com.lmabbe.core.common.entitys.SysUserRole;
import com.lmabbe.core.common.interfaces.SysRoleResourceService;
import com.lmabbe.core.common.interfaces.SysUserRoleService;
import com.lmabbe.core.service.dao.SysRoleResourceMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lmabbe
 */
@Slf4j
@DubboService(provider = "simple-dubbo")
@AllArgsConstructor
public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMapper, SysRoleResource> implements SysRoleResourceService {

    private SysUserRoleService sysUserRoleService;

    @Override
    public void bindResources(Long roleId, List<Long> resourcesIds) {

        this.baseMapper.delete(lambdaQuery().eq(SysRoleResource::getRoleId, roleId));

        resourcesIds.parallelStream().map(r -> this.baseMapper.insert(new SysRoleResource(r, roleId)));
    }


    @Override
    public List<SysResource> findResourceByUserId(Long userId) {
        List<SysUserRole> userRoles = sysUserRoleService.getRoleByUserId(userId);
        return baseMapper.findResourceByRoleId(userRoles.stream().map(s -> s.getRoleId()).collect(Collectors.toList()));
    }
}
